<?php
namespace app\admin\controller;

use app\admin\model\Kf;

class KfCate extends Common{
    public function _initialize()
    {
        parent::_initialize();
    }
    //客服分类即职责
    public function index()
    {
        $this->assign([
            'cat_list' => TreeToTwoArr(get_attr($this->getKfCateAll(),0,'id','parent_id'),'children')
        ]);
        return $this->fetch();
    }

    //客服分类即职责
    public function add()
    {
        $this->assign([
            'cat_list' => GetTree($this->getKfCateAll()),
        ]);
        return $this->fetch();
    }

    //添加一个客服分类职责
    public function insert()
    {
        /**
         * 参数说明
         * name 分类名
         * description 描述
         * parent_id 分类id
         */
        $post = input('post.');
        $KfCate = new \app\admin\model\KfCate();
        $result = $KfCate->addCate($post);

        $this->success($result['msg'],url('index'));
    }

    //编辑一个客服分类职责
    public function edit()
    {
        /**
         * 参数说明
         * param.id
         */
        $id = input('param.id');
        $KfCate = new \app\admin\model\KfCate();
        $data = $KfCate->info(['id'=>$id]);
        if($data['error'] !=0 || empty($data['data'])) $this->error('不存在该职责');

        $this->assign([
            'cat_list' => GetTree($this->getKfCateAll()),
            'id' =>$id,
            'info'=>$data['data']
        ]);
        return $this->fetch();
    }

    public function getKfCateAll()
    {
        $KfCate = new \app\admin\model\KfCate();
        return $KfCate->all();
    }

    //ajax删除客服
    public function del()
    {
        /**
         * 参数说明
         * param.id
         */
        $input  = !empty(input('param.id')) ? [input('param.id')] : null;
        if(empty($input))
        {
            return ['error'=>1,'msg'=>'没有选中职责'];
        }
        $KfCateModel = new \app\admin\model\KfCate();
        return $KfCateModel->del($input);
    }

    //删除多个客服
    public function delAll()
    {
        /**
         * 参数说明
         * post.id[]
         */
        $input = input('post.');
        if(!isset($input['id']) || empty($input['id']))
        {
            $this->error("没有选中职责");
        }
        $KfCateModel = new \app\admin\model\KfCate();
        $result = $KfCateModel->del($input['id']);
        $this->success($result['msg'],url('index'));
    }

    //更新一个职责
    public function update()
    {
        /**
         * 参数说明
         * name 职责名称
         * description 描述
         * parent_id 父职责id
         */
        $input = input('post.');
        $KfCateModel = new \app\admin\model\KfCate();
        $result = $KfCateModel->edit($input);
        if($result['error'] != 0)
        {
            $this->error($result['msg']);
        }
        else
        {
            $this->success($result['msg'],url('index'));
        }
    }
}